import { _decorator, Component, find, instantiate, Node, Prefab, v2, v3, Vec2 } from 'cc';
import { Knight } from './Knight';
import ResManager from '../../script/common/ResManager';
import { PrefabEnum } from '../../script/common/PrefabEnum';
import { UserInfo } from '../common/UserInfo';
const { ccclass, property } = _decorator;

@ccclass('March')
export class March extends Component {
    @property([Node])
    knightList: Node[] = []


    /**起始点 */
    startPos:Vec2 = v2(0, 0)
    /**终点 */
    endPos:Vec2 = v2(0, 0)
    /**方向 */
    dir:Vec2 = v2(0, 0)
    /**到达时间 */
    endTime:number = 0
    /**开始时间 */
    startTime:number = 0
    /**行军速度 */
    speed:number = 1

    /**
     * 
     * @param sPos 起始点，基于世界坐标
     * @param ePos 终点，基于世界坐标
     * @param startTime 
     * @param endTime 
     * @returns 
     */
    static async create(sPos:Vec2, ePos:Vec2, startTime:number, endTime:number):Promise<Node>{
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.March)
       
        let node = instantiate(prefab)
        find('Canvas').addChild(node)
        node.worldPosition = v3(sPos.x, sPos.y , 0)
        let march = node.getComponent(March)
        march.setData(sPos, ePos, startTime, endTime)
       
        return node
    }

    start() {

    }

    update(deltaTime: number) {
        if(this.endTime > 0){
            let now = new Date().getTime() / 1000
            let time = this.endTime - (UserInfo.instance.offsetTime / 1000)
            if(time < now -2){
                this.node.destroy()
            }
        }


        let dir3 = v3(this.dir.x, this.dir.y, 0)
        this.node.translate(dir3.multiplyScalar(this.speed * deltaTime))

        // let wpos = this.node.worldPosition
        // let distance = Vec2.distance(this.endPos,v2(wpos.x, wpos.y))
        // if(distance < 50){
        //     this.node.destroy()
        // }

    }

    setData(sPos:Vec2, ePos:Vec2, startTime:number, endTime:number){
        this.startPos = sPos
        this.endPos = ePos
        this.startTime = startTime
        this.endTime = endTime
        // 计算出方向
        this.dir = ePos.clone().subtract(sPos).normalize()
        // 计算出速度
        let distance = Vec2.distance(sPos, ePos)
        this.speed = distance / (endTime - startTime)
        
        // 计算出骑兵当前的位置
        let now = new Date().getTime() / 1000 
        now += UserInfo.instance.offsetTime / 1000
        let pro = (now - startTime) / (endTime - startTime)
        let offsetPos = v3(this.dir.x, this.dir.y, 0).multiplyScalar(pro * distance)
        
        this.node.translate(offsetPos)
        

        // 设置三个骑兵的位置和动画
        for(let node of this.knightList){
            node.getComponent(Knight).setData(sPos, ePos)
        }

        this.setKnightPos(this.knightList[0], 135)
        this.setKnightPos(this.knightList[2], -135)
       
    }

    setKnightPos(knight:Node,degree:number){
        let pos = this.dir.clone().rotate(degree / 180 * Math.PI)
        pos.normalize().multiplyScalar(80)
        knight.position = v3(pos.x, pos.y, 0)
    }
}


